﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class PlayerList
    {
        #region Properties

        [DataMember]
        public List<UriLink> Players = new List<UriLink>();

        #endregion

        public PlayerList Load(int teamId)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var players = (
                    from ft in context.FieldedTeams
                    from a in context.Appearances
                    from p in context.People
                    orderby p.LastName
                    where a.TeamId == ft.FieldedTeamId && p.PersonId == a.PersonId
                        && ft.FieldedTeamId == teamId
                    select new
                    {
                        p.FirstName,
                        p.LastName,
                        p.PersonId
                    }).ToList();

                foreach (var itm in players)
                {
                    Players.Add(new UriLink(string.Format(Constants.baseUrl + "player/{0}", itm.PersonId), string.Format("{0} {1}", itm.FirstName, itm.LastName)));
                }
                return this;
            }            

        }

    }
}